package com.ruoyi.project.recommend.recommend.service.impl;

import java.util.List;

import com.ruoyi.project.question.compose.domain.QuestionnaireInformantConn;
import com.ruoyi.project.question.compose.mapper.QuestionnaireInformantConnMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.recommend.recommend.mapper.RecommendMapper;
import com.ruoyi.project.recommend.recommend.domain.Recommend;
import com.ruoyi.project.recommend.recommend.service.IRecommendService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 干部推荐Service业务层处理
 * 
 * @author haohui
 * @date 2021-12-16
 */
@Service
public class RecommendServiceImpl implements IRecommendService 
{
    @Autowired
    private RecommendMapper recommendMapper;

    @Autowired
    private QuestionnaireInformantConnMapper questionnaireInformantConnMapper;
    /**
     * 查询干部推荐
     * 
     * @param id 干部推荐主键
     * @return 干部推荐
     */
    @Override
    public Recommend selectRecommendById(Long id)
    {
        return recommendMapper.selectRecommendById(id);
    }

    /**
     * 查询干部推荐列表
     * 
     * @param recommend 干部推荐
     * @return 干部推荐
     */
    @Override
    public List<Recommend> selectRecommendList(Recommend recommend)
    {
        return recommendMapper.selectRecommendList(recommend);
    }

    /**
     * 新增干部推荐
     * 
     * @param recommend 干部推荐
     * @return 结果
     */
    @Override
    public int insertRecommend(Recommend recommend)
    {

        QuestionnaireInformantConn conn = new QuestionnaireInformantConn(null, recommend.getRefereeId(), null);
        List<QuestionnaireInformantConn> connList = questionnaireInformantConnMapper.selectQuestionnaireInformantConnList(conn);
        long score=0L;
        for (QuestionnaireInformantConn informantConn : connList) {
            if(informantConn.getInformantId().equals(recommend.getRefereeId())){
                score+=Integer.parseInt(informantConn.getGrade());
            }
        }
        recommend.setGrade(score);
        return recommendMapper.insertRecommend(recommend);
    }

    /**
     * 修改干部推荐
     * 
     * @param recommend 干部推荐
     * @return 结果
     */
    @Override
    public int updateRecommend(Recommend recommend)
    {
        Recommend recommend1 = recommendMapper.selectRecommendById(recommend.getId());
        if(!recommend.getRefereeId().equals(recommend1.getRefereeId())){
            QuestionnaireInformantConn conn = new QuestionnaireInformantConn(null, recommend.getRefereeId(), null);
            List<QuestionnaireInformantConn> connList = questionnaireInformantConnMapper.selectQuestionnaireInformantConnList(conn);
            long score=0L;
            for (QuestionnaireInformantConn informantConn : connList) {
                if(informantConn.getInformantId().equals(recommend.getRefereeId())){
                    score+=Integer.parseInt(informantConn.getGrade());
                }
            }
            recommend.setGrade(score);
        }
        return recommendMapper.updateRecommend(recommend);
    }

    /**
     * 批量删除干部推荐
     * 
     * @param ids 需要删除的干部推荐主键
     * @return 结果
     */
    @Override
    public int deleteRecommendByIds(String ids)
    {
        return recommendMapper.deleteRecommendByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除干部推荐信息
     * 
     * @param id 干部推荐主键
     * @return 结果
     */
    @Override
    public int deleteRecommendById(Long id)
    {
        return recommendMapper.deleteRecommendById(id);
    }
}
